//
//  JJMessageCellLabelManager.swift
//  swiftProject
//
//  Created by yangjian on 2021/12/9.
//

import UIKit
import ObjectiveC.runtime
import RxSwift
import Masonry

class IMMsgCellMenuModel {
    var id = 0
    var title = ""
    init(id: Int = 0, title: String = "") {
        self.id = id
        self.title = title
    }
}

class IMMsgCellMenuItem : UIControl {
    var id : Int {
        get{
            self.tag
        }
        set{
            self.tag = newValue
        }
    }
    let titleLabel = UILabel()
    init(id : Int, title:String){
        super.init(frame: CGRect.zero)
        self.id = id
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.center.equalTo(self)
        }
        self.titleLabel.font = UIFont.systemFont(ofSize: 13, weight: .regular)
        self.titleLabel.textColor = UIColor.orange
        self.titleLabel.text = title
//        self.setTitle(title, for: .normal)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


let messageCell_menuItemHorizontalCount = 5

let messageCell_menuItemWidth : CGFloat = 60
let messageCell_menuItemHeight : CGFloat = 40

class IMMsgCellMenuView : UIView {
    var scrollView = UIScrollView.init(frame: CGRect.zero)
    var stackView = UIStackView.init(frame: CGRect.zero)
    var arrowView = UIImageView.init(frame: CGRect.zero)
    var contentView = UIView.init(frame: CGRect.zero)
    var currentItems = [IMMsgCellMenuItem].init()
    
    var currentWidth : CGFloat = 0.0
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    init(){
        super.init(frame: CGRect.zero)
        self.backgroundColor = UIColor.lightGray
        self.layer.cornerRadius = 6
        self.layer.masksToBounds = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        self.addSubview(scrollView)
        self.scrollView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets.zero)
            make.height.equalTo(messageCell_menuItemHeight)
            make.width.equalTo(messageCell_menuItemWidth)
        }
        
        stackView.axis = .horizontal
        stackView.spacing = 0.5
        stackView.alignment = .leading
        stackView.distribution = .fillEqually
        self.scrollView.addSubview(self.stackView)
        self.stackView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets.zero)
            make.height.equalTo(messageCell_menuItemHeight)
        }
    }
    func fitArrowView(){
        
    }
    
    func removeItems(){
        for item in currentItems {
            self.stackView.removeArrangedSubview(item)
            item.removeFromSuperview()
        }
        currentItems.removeAll()
    }
    func setItems(items:[IMMsgCellMenuItem]){

        self.removeItems()
        
        for item in items {
            self.currentItems.append(item)
            self.stackView.addArrangedSubview(item)
            item.snp.makeConstraints { make in
                make.width.equalTo(messageCell_menuItemWidth)
                make.height.equalTo(messageCell_menuItemHeight)
            }
        }
        var width : CGFloat = 0
        if items.count > 4 {
            width = 4.5 * messageCell_menuItemWidth;
        }else {
            width = CGFloat(items.count) * messageCell_menuItemWidth;
        }
        self.scrollView.snp.updateConstraints { make in
            make.width.equalTo(width)
        }
        currentWidth = width;
    }
}

class IMMsgCellMenuManager: NSObject {
    static let share = IMMsgCellMenuManager()

//    private static let jj = JJMessageCellMenuManager.init()
//    class func shared() -> JJMessageCellMenuManager{
//        return jj;
//    }
//    var tempBag = DisposeBag()
    var showing = false
    var menuView = IMMsgCellMenuView()
    func showMenu(superView:UIView,targetView:UIView,items:[IMMsgCellMenuItem]){
        self.hideMenu()
        showing = true
        superView.addSubview(self.menuView)
        self.menuView.setItems(items: items)
        
//         (screen_width() - menuView.currentWidth) / 2
        
//        let targetConvertFrame = targetView.convert(targetView.frame, to: superView)
//        
//        var left = targetConvertFrame.origin.x
//        var right = targetConvertFrame.origin.x + targetConvertFrame.size.width
//        if(left < 40){
//            left = 40
//        }
//        if(right >)
        
        
        menuView.snp.remakeConstraints { make in
            make.centerX.equalTo(targetView).priority(.low)
            make.bottom.equalTo(targetView.snp.top)
            make.left.greaterThanOrEqualTo(superView.snp.left).offset(40).priority(.required)
            make.right.lessThanOrEqualTo(superView.snp.right).offset(-40).priority(.required)
        }
//        menuView.mas_remakeConstraints { make in
//            guard let make else{return}
//            make.centerX.equalTo()(targetView)?.priorityLow()
//            make.bottom.equalTo()(targetView.mas_bottom)
//            make.left.greaterThanOrEqualTo()(superView.mas_left)
////                .offset()(40).priorityHigh()
//            make.right.lessThanOrEqualTo()(superView.mas_right)
//        }
//        print(superView.frame)
//        print(menuView.frame)
//        print(screen_width())
//        print("==========")
    }
    
    func hideMenu(){
        if showing {
//            self.tempBag = DisposeBag()
            self.menuView.removeFromSuperview()
            self.menuView.removeItems()
        }
    }
}
